# -*- coding: utf-8 -*-
# @Time    : 2024/11/7 下午1:56
# @Author  : xiaowanzi
# @File    : jenkins_show.py
import os
import sys
import re


def jenkins_show_report(filename, actuator, environment=None):
    """ 该功能实现在jenkins简单展示测试结果
    :param environment: 环境
    :type filename: 文件名
    :return 测试报告简况
    """
    if actuator == "unittest":
        if environment == None:
            file_path = os.path.split(os.path.realpath(sys.argv[0]))[0].replace("/", "\\") + \
                        "\\report\\%s.html" % filename
            filename_url = "ws/report/%s.html" % filename
        else:
            file_path = os.path.split(os.path.realpath(sys.argv[0]))[0].replace("/", "\\") + \
                        "\\report\\%s\\%s.html" % (environment, filename)
            filename_url = "ws/report/%s/%s.html" % (environment, filename)
        with open(file_path, "r", encoding="utf-8") as file:
            f = file.read()
            testAll = re.search(r'"testAll": (.*?),', f)
            testPass = re.search(r'"testPass": (.*?),', f)
            testFail = re.search(r'"testFail": (.*?),', f)
        return '"{}Test report","{}","{}","{}","{}","{}"'.format("none", 0, 0, 0, 0, 0) + \
               ":{}name:{}all:{}pass:{}fail:{}".format("block", filename_url, testAll.group(1), testPass.group(1),
                                                       testFail.group(1))
    else:
        file_path = os.path.split(os.path.realpath(sys.argv[0]))[0]
        os.system(r"allure generate {}/allure-results/ -o {}/allure-report/ --clean ".format(file_path, file_path,file_path))
        with open(r"{}/allure-report/export/prometheusData.txt".format(file_path), "r", encoding="utf-8") as file:
            f = file.read()
            failed = re.search(r'launch_status_failed (.*)', f)
            broken = re.search(r'launch_status_broken (.*)', f)
            passed = re.search(r'launch_status_passed (.*)', f)
            skipped = re.search(r'launch_status_skipped (.*)', f)
            unknown = re.search(r'launch_status_unknown (.*)', f)

        return '"{}Test report","{}","{}","{}","{}","{}"'.format("block", failed.group(1), (int(failed.group(1)) + int(broken.group(1))),
                                                                 passed.group(1), skipped.group(1),
                                                                 (int(failed.group(1)) + int(broken.group(1)) + int(
                                                                     passed.group(1)) + int(skipped.group(1)) + int(
                                                                     unknown.group(1)))) + \
               ":{}name:{}all:{}pass:{}fail:{}".format("none", 0, 0, 0, 0)
